package com.icinema.main;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.icinema.client.ICinema;
import com.icinema.client.UserService;
import com.icinema.client.UserServiceAsync;
import com.icinema.vo.User;

public class UserRegisterView extends Composite {
	TextBox txtEmail = new TextBox();
	PasswordTextBox txtPassword = new PasswordTextBox();
	Label lblStatus = new Label("");
	ICinema iCinema;
	private final UserServiceAsync userService = GWT
	.create(UserService.class);		
	@SuppressWarnings("deprecation")
	public UserRegisterView(){
		FlexTable formPanel = new FlexTable();
		initWidget(formPanel);
		formPanel.setText(0,0,"Email:");
		formPanel.setWidget(0,1,txtEmail);
		formPanel.setText(1,0,"Password:");
		formPanel.setWidget(1,1,txtPassword);
		formPanel.setWidget(3,1,lblStatus);
		Hyperlink backLink = new Hyperlink();
		backLink.setText("Back");
		backLink.setStyleName("hyper-link");
		formPanel.setWidget(4,1,backLink);
		Button btnRegister = new Button("Register");
		formPanel.setWidget(2,1,btnRegister);
		btnRegister.addMouseUpHandler(new MouseUpHandler(){
			@Override
			public void onMouseUp(MouseUpEvent event) {
				final User user = new User();
				user.setEmail(txtEmail.getText());
				user.setPassword(txtPassword.getText());
				user.setStatus("P");
				userService.addUser(user, new AsyncCallback<String>(){

					@Override
					public void onFailure(Throwable caught) {
						
					}

					@Override
					public void onSuccess(String result) {
						userService.sendEmail(user.getEmail(), result,new AsyncCallback<Void>(){
							@Override
							public void onFailure(Throwable caught) {
								
							}

							@Override
							public void onSuccess(Void result) {
								lblStatus.setText("We've Send the Confirmation Email to Your Email");								
							}
							
						});
					}
					
				});
			}
			
		});
		backLink.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				iCinema.showHomeViewCallBack();
			}
		});		
		
	}
	public void setICinema(ICinema cinema) {
		iCinema = cinema;
	}
	
}
